package com.softeem.oss_music.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.softeem.oss_music.comm.R;
import com.softeem.oss_music.entity.MusicListDetails;
import com.softeem.oss_music.service.MusicListDetailsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "歌单详情管理")
@RestController
public class MusicListDetailsController {
    @Autowired
    private MusicListDetailsService musicListDetailsService;

    @Operation(summary = "获取歌单中的歌曲列表", description = "根据歌单ID查询歌单中的所有歌曲")
    @Parameters({
            @Parameter(name = "listId", description = "歌单ID")
    })
    @GetMapping("/musicListDetails/list")
    public R list(Integer listId) {
        if (listId == null) {
            return R.error().setMessage("歌单ID不能为空");
        }
        QueryWrapper<MusicListDetails> query = Wrappers.query(MusicListDetails.class);
        query.eq("list_id", listId);
        List<MusicListDetails> list = musicListDetailsService.list(query);
        return R.success().setData(list);
    }

    @Operation(summary = "添加歌曲到歌单", description = "将歌曲添加到指定歌单")
    @PostMapping("/musicListDetails/add")
    public R add(@RequestBody MusicListDetails details) {
        if (details.getListId() == null || details.getMusicId() == null) {
            return R.error().setMessage("歌单ID和歌曲ID不能为空");
        }

        // 检查是否已存在相同的关联关系
        QueryWrapper<MusicListDetails> query = Wrappers.query(MusicListDetails.class);
        query.eq("list_id", details.getListId())
             .eq("music_id", details.getMusicId());
        if (musicListDetailsService.count(query) > 0) {
            return R.error().setMessage("歌曲已在歌单中");
        }

        boolean result = musicListDetailsService.save(details);
        return result ? R.success().setMessage("添加成功").setData(details) : R.error().setMessage("添加失败");
    }

    @Operation(summary = "从歌单中删除歌曲", description = "根据歌单ID和歌曲ID从歌单中删除歌曲")
    @Parameters({
            @Parameter(name = "listId", description = "歌单ID"),
            @Parameter(name = "musicId", description = "歌曲ID")
    })
    @DeleteMapping("/musicListDetails/delete")
    public R delete(Integer listId, Integer musicId) {
        if (listId == null || musicId == null) {
            return R.error().setMessage("歌单ID和歌曲ID不能为空");
        }
        QueryWrapper<MusicListDetails> query = Wrappers.query(MusicListDetails.class);
        query.eq("list_id", listId)
             .eq("music_id", musicId);
        boolean result = musicListDetailsService.remove(query);
        return result ? R.success().setMessage("删除成功") : R.error().setMessage("删除失败");
    }

    @Operation(summary = "清空歌单", description = "删除歌单中的所有歌曲")
    @Parameters({
            @Parameter(name = "listId", description = "歌单ID")
    })
    @DeleteMapping("/musicListDetails/clear")
    public R clear(Integer listId) {
        if (listId == null) {
            return R.error().setMessage("歌单ID不能为空");
        }
        QueryWrapper<MusicListDetails> query = Wrappers.query(MusicListDetails.class);
        query.eq("list_id", listId);
        boolean result = musicListDetailsService.remove(query);
        return result ? R.success().setMessage("歌单已清空") : R.error().setMessage("清空失败");
    }
}